package com.wswy.wzcx.widget.home;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;

import com.blankj.utilcode.util.SizeUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.ui.other.LayoutHelper;
import com.wswy.wzcx.ui.other.ViewHelp;
import com.wswy.wzcx.widget.shadow.ShadowFrameLayout;

/**
 * Created by wangduheng26 on 2018/3/6.
 * wangduheng26@gmail.com
 */

public class RubbishFrameLayout extends ShadowFrameLayout {
    public static final int TYPE_JZCF = 1;
    public static final int TYPE_WZDB = 2;
    ImageView leftImage, rightImage;
    private int mType;

    public RubbishFrameLayout(@NonNull Context context) {
        super(context);
        init(context,null);
    }
    private void init(Context context,@Nullable AttributeSet attrs) {
        setMinimumHeight(SizeUtils.dp2px(110));
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RubbishFrameLayout);
        mType= typedArray.getColor(R.styleable.RubbishFrameLayout_rf_type, 0);
        typedArray.recycle();
        initChild();
    }


    public RubbishFrameLayout(@NonNull Context context, int type) {
        super(context);
        init(context,null);
        mType = type;
        initChild();

    }

    public RubbishFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }

    public RubbishFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);

    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RubbishFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void initChild() {
        if (mType == 0) {
            removeAllViews();
            return;
        }
        int leftDrawableId = 0;
        int rightDrawableId = 0;
        if (mType == TYPE_JZCF) {
            leftDrawableId = R.drawable.cardbg_green_left;
            rightDrawableId = R.drawable.cardbg_green_right;
        }else if(mType==TYPE_WZDB){
            leftDrawableId = R.drawable.cardbg_green_left;
            rightDrawableId = R.drawable.cardbg_green_right;
        }
        Context context = getContext();
        leftImage = new ImageView(context);
        leftImage.setImageResource(leftDrawableId);
        LayoutParams params = LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT);
        params.gravity = Gravity.LEFT | Gravity.BOTTOM;
        LayoutHelper.setMargin(params, 5, 0, 0, 0);
        addViewInLayout(leftImage, -1, params);
        rightImage = new ImageView(context);
        rightImage.setImageResource(rightDrawableId);
        params = LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT);
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        LayoutHelper.setMargin(params, 0, 0, 5, 0);
        addView(rightImage, -1, params);

    }

    public void changeType(int type) {
        mType = type;
        initChild();
    }

}
